﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BPMS.Core.Builders
{
    public class UpdateStatementBuilder : BuilderBase
    {
        private Type setClauseType;
        public UpdateStatementBuilder(Type entityType)
            : base(entityType)
        { }

        public UpdateStatementBuilder(Type entityType, object setClause)
            : this(entityType)
        {
            if (null != setClause)
                setClauseType = setClause.GetType();
        }

        public override string GetOperateStatement()
        {
            return " UPDATE " + DataRecordName
                    + BuildSetClause()
                   + " WHERE 1=1 ";
        }

        public string BuildSetClause()
        {
            if (setClauseType != null)
            {
                return " SET " + string.Join(",", PropertyList(setClauseType).Select(GetCondtionClauseItem));
            }
            return " SET " + string.Join(",", DataFieldList.Where(item => !item.IsIdentity).Select(GetCondtionClauseItem));
        }
    }
}
